package com.baymax.test.scene.internal

import android.app.Activity
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import okio.Buffer
import java.io.OutputStream
import java.io.PrintWriter

private val bf = Buffer()
private val executor = Dispatchers.IO.limitedParallelism(1)
val out = object : OutputStream() {
    override fun write(b: Int) {
        System.out.write(b)
        bf.outputStream().write(b)
    }
}
private val runner = executor.asExecutor()
private val pw = PrintWriter(out)


fun Activity.dump() {
    pw.doDump {
        this.dump("dump_activity", null, pw, null)
    }
}


private inline fun PrintWriter.doDump(crossinline ac: () -> Unit) {
    runner.execute {
        Log.d("dump", "startDump")
        ac()
        this.flush()
        Log.d("dump", "endDump ")
    }
}
